<?php
namespace App\Http\Controllers\Wechat;

use App\Models\Activity;
use App\Models\ActivityMember;
use App\Models\Goods;
use App\Http\Controllers\Controller;
use App\Services\CommonService;
use App\Services\ActivityService;
use App\Services\GoodsService;
use Illuminate\Http\Request;

class ActivityController extends BaseController
{
    //领券
    public function setMemberActivity(Request $request)
    {
        $activity_id = $request->input('activity_id');
        $user = $this->member;
        
        $activity = Activity::where('id',$activity_id)->where('is_show',ActivityService::ACTIVITY_SHOW_YES)->first();
        
        $activity = $activity ? $activity->toArray() : [];
        
        if(empty($activity)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该活动已过期');
        }
        $valid = $activity['valid'];
        $end_at = time()+$valid*24*60*60;
        $data = [];
        $data['activity_id'] = $activity_id;
        $data['uid'] = $user['id'];
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['end_at'] = date('Y-m-d H:i:s',$end_at);
        $data['is_show'] = ActivityService::ACTIVITY_MEMBER_YES;
        $is_activity = ActivityMember::where('activity_id',$activity_id)
                    ->where('uid',$user['id'])
                    ->first();
        $is_activity = $is_activity ? $is_activity->toArray() : [];      
        if($is_activity){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '已经领取该券');
        }
        $res = ActivityMember::insert($data);
        if($res){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'领券成功',$res);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '领取状态错误');
        }
        
    }
    
    //展示券
    public function getActivity(Request $request)
    {
        $input = $request->input();
        $user = $this->member;
        $activity = Activity::from('sp_activity as a')
                ->leftjoin('sp_activity_member as am','a.id','=','am.activity_id')
                ->selectRaw("
                    a.id,a.title,a.money,a.full,a.valid,a.info,a.remark,a.is_code
                    ,GROUP_CONCAT(am.uid) as uid
                ")
                ->where('a.is_show',ActivityService::ACTIVITY_SHOW_YES)
                ->groupby('a.id')
                ->get();
        
        $activity = $activity ? $activity->toArray() : [];
        foreach($activity as $key=>$value){
            $uid = explode(',',$value['uid']);
            if(in_array($user['id'],$uid)){
                $activity[$key]['is_have'] = ActivityService::ACTIVITY_MEMBER_YES;
            }else{
                $activity[$key]['is_have'] = ActivityService::ACTIVITY_MEMBER_NO;
            }
        }
        
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'查询成功',$activity);
        
        
    }

    //展示券商品
    public function getActivityGoods(Request $request)
    {
        $activity_id = $request->input('activity_id');
        
        $page = $request->input('page');
        
        $pageSize = $request->input('pageSize',100);
        
        $activity_goods = Goods::from('sp_goods_2 as g')
            ->leftjoin('sp_activity_goods as ag','g.id','=','ag.goods_id')
            ->selectRaw("
                g.title,g.id,g.extension_title,g.rent_label,g.new_grade,g.imgurl,g.min_price,g.min_lease_term,g.min_price_g
            ")
            ->where('ag.activity_id',$activity_id)
            ->where('g.is_show',GoodsService::GOODS_IS_SHOW_YES)
            ->where('ag.is_show',ActivityService::ACTIVITY_SHOW_YES)
            ->groupby('g.id')
            ->orderby('g.create_time','desc')
            ->paginate($pageSize);
        $activity_goods = $activity_goods ? $activity_goods->toArray() : [];
        
        $activity = Activity::where('id',$activity_id)->first();
        $activity = $activity ? $activity->toArray() : [];
        
        foreach($activity_goods['data'] as $key=>$value){
            
            $min_price = $value['min_price'];
            
            if($activity['is_code'] == ActivityService::ACTIVITY_CODE_ONE){
                
                //满减券
                if($min_price>=$activity['full']){
                    
                    $activity_goods['data'][$key]['firstRent_new'] = $min_price-$activity['money'];
                    
                }else{
                    $activity_goods['data'][$key]['firstRent_new'] = $min_price;
                    
                }
            }else if($activity['is_code'] == ActivityService::ACTIVITY_CODE_TWO){
                //折扣券
                $money = $activity['money']/10;
                
                $activity_goods['data'][$key]['firstRent_new'] = $min_price * $money;
                
                
            }else if($activity['is_code'] == ActivityService::ACTIVITY_CODE_THREE){
                //固定金额券
                $activity_goods['data'][$key]['firstRent_new'] = $activity['money'];
                
            } 
            $activity_goods['data'][$key]['firstRent_new'] = sprintf("%.2f",$activity_goods['data'][$key]['firstRent_new']);
        }
        
        $activity['activity_goods'] = $activity_goods;
        
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $activity);
    }
}
